import bpy


import 节点.全局控制.Node.节点和插座基类.bl_节点基类 as 基类


import 物体.物体管理.物体数据 as 网格数据
from 物体.物体管理 import engine_物体创建
from 节点.Core import bl_节点构建模板
from 节点.Core import bl_UI配色

import engine.initLIB.load_dll as engineLib
import ctypes










engineLib.flexCAPI.f_创建取Bl网格节点.restype = ctypes.c_bool














def f_update_取网格配置修改(self, context):
	data = [list('参数配置')]
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
	if context :
		self.id_data.f_节点数据更新_更新树([self])

def f_update_取网格变换开启修改(self, context):
	data = [list('变换配置'), int(self.m_应用位置), int(self.m_应用旋转), int(self.m_应用缩放)]
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
	if context :
		self.id_data.f_节点数据更新_更新树([self])

class C_Node_取网格(基类.C_Node_基类):
	bl_dname = 'bl_node_取网格'
	bl_label = '取网格'
	
	m_是否分割成三角形 = bpy.props.BoolProperty(default=True, update=f_update_取网格配置修改)
	m_应用位置 = bpy.props.BoolProperty(default=False, update=f_update_取网格变换开启修改)
	m_应用旋转 = bpy.props.BoolProperty(default=False, update=f_update_取网格变换开启修改)
	m_应用缩放 = bpy.props.BoolProperty(default=False, update=f_update_取网格变换开启修改)
	m_应用矩阵 = bpy.props.BoolProperty(default=False, update=f_update_取网格配置修改)
	m_封包 = bpy.props.BoolProperty(default=True, update=f_update_取网格配置修改)
	def init(self, context):
		super(C_Node_取网格, self).f_init(context)
		self.inputs.new('C_Socket_物体_Array1D', name='物体')
		self.outputs.new('C_Socket_网格_Array1D', name='网格')
		#执行DLL函数
		self.f_toCPP(self.id_data)
		
	def draw_buttons(self, context, layout):
		layout.prop(self, "m_是否分割成三角形", text="分割成三角形")
		layout.prop(self, "m_应用位置", text="应用位置")
		layout.prop(self, "m_应用旋转", text="应用旋转")
		layout.prop(self, "m_应用缩放", text="应用缩放")
		layout.prop(self, "m_应用矩阵", text="应用矩阵")
		layout.prop(self, "m_封包", text="封包")
		
	def f_toCPP(self, tree):
		新创建 = engineLib.flexCAPI.f_创建取Bl网格节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 新创建 :
			self.use_custom_color = True
			self.color = bl_UI配色.g_网格类节点颜色
			
			f_update_取网格变换开启修改(self, None)


	def f_update(self):
		if len(self.inputs) < 2 : return;
		#print('self.m_是否分割成三角形 ============ ', self.inputs, self.name)
		objs = self.inputs['物体'].f_getData()
		#print('self.m_是否分割成三角形 ============ ', objs, self.name)
		if len(objs) <= 0 : return
		
		list_data = [list('网格数据')]
		for obj in objs:
			MESH = 网格数据.f_get物体网格数据(obj, bpy.context, 导出三角面=self.m_是否分割成三角形, 应用矩阵=self.m_应用矩阵)
			list_data.append(MESH)
				#engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object());
			#else:
			#	data = 网格数据.f_get物体网格数据(obj, bpy.context, 导出三角面=self.m_是否分割成三角形, 应用矩阵=self.m_应用矩阵, 封包=self.m_封包)
			#	data.insert(0, list(''))
			#	list_data.append(data)
		#print('self.m_是否分割成三角形 ============ ', self.m_是否分割成三角形, len(list_data), self.name)
		engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(list_data));
		
		
		
class C_NODE_MENU_取网格(bpy.types.Operator):
	bl_idname = 'node.global_node_get_mesh'
	bl_label = '取网格'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		node = bpy.ops.node.add_node(type='C_Node_取网格', use_transform=True)
		print('create node = ', node)
		return {'FINISHED'}










'''import threading
import time

def prod():
    print(threading.current_thread().name, "Starting")

    # do something vaguely useful
    import bpy
    from mathutils import Vector
    from random import random

    prod_vec = Vector((random() - 0.5, random() - 0.5, random() - 0.5))
    print("Prodding", prod_vec)
    bpy.data.objects["Cube"].location += prod_vec
    time.sleep(random() + 1.0)
    # finish

    print(threading.current_thread().name, "Exiting")

threads = [threading.Thread(name="Prod %d" % i, target=prod) for i in range(10)]


print("Starting threads...")

for t in threads:
    t.start()

print("Waiting for threads to finish...")

for t in threads:
    t.join()'''














classes = (
	C_Node_取网格,
	C_NODE_MENU_取网格,

)

def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)







